'use strict';
/**
 * 用户数据
 */
const Service = require('egg').Service;

class AppointmentService extends Service {
  async getHospitalInformation(data) {
    const result = await this.app.mysql.select('tb_hospitalinformation', {
      where: { f_organizationid: data.organizationId },
    });
    if (result.length === 0) {
      throw new Error('f_organizationid不存在');
    }
    return {
      organizationId: result[0].f_organizationId,
      organizationName: result[0].f_organizationName,
      telephone: result[0].f_telephone,
      address: result[0].f_address,
      organizationInformation: result[0].f_organizationInformation,
    };
  }

  async listDepartment(data) {
    let result;
    if (data.organizationId === '1') {
      if (data.outpatientType === 1) {
        if (data.departmentId === '') {
          result = await this.app.mysql.select('tb_sysbase_depart', {
            where: { f_disable: 0 },
          });
        } else {
          result = await this.app.mysql.select('tb_sysbase_depart', {
            where: { f_disable: 0, f_departmentId: data.departmentId },
          });
        }
        const ary_pid = [];
        result.forEach(e => {
          if (ary_pid.indexOf(e.f_parentID) === -1) {
            ary_pid.push(e.f_parentID);
          }
        });
        const presult = await this.app.mysql.select('tb_sysbase_departclass', {
          where: { f_id: ary_pid, f_disable: 0 },
        });
        const m = [];
        presult.forEach(pe => {
          const departmentList = [];
          result.forEach(e => {
            if (e.f_parentID === pe.f_id) {
              departmentList.push({
                departmentId: e.f_departmentId,
                departmentName: e.f_departmentName,
                telephone: e.f_telephone,
                address: e.f_address,
                keyword: e.f_keyword,
              });
            }
          });
          m.push({
            parentDepartmentId: pe.f_id,
            parentDepartmentName: pe.f_name,
            departmentSummary: pe.f_summary,
            departmentlogo: pe.f_logo,
            departmentList,
          });
        });
        return m;
      }
    }
    // return [{
    //   parentDepartmentId: '1011',
    //   parentDepartmentName: '内分泌科',
    //   departmentSummary: null,
    //   departmentlogo: '',
    //   departmentList: [{
    //     departmentId: '107',
    //     departmentName: '内分泌科',
    //     telephone: null,
    //     address: null,
    //     keyWord: '内分泌科',
    //   },
    //   {
    //     departmentId: '108',
    //     departmentName: '内分泌科专家',
    //     telephone: null,
    //     address: null,
    //     keyWord: '内分泌科',
    //   },
    //   ],
    // },
    // {
    //   parentDepartmentId: '1021',
    //   parentDepartmentName: '骨科',
    //   departmentSummary: null,
    //   departmentlogo: '',
    //   departmentList: [{
    //     departmentId: '114',
    //     departmentName: '骨科',
    //     telephone: null,
    //     address: null,
    //     keyWord: '骨科',
    //   }],
    // }];
  }

  async listNumber(data) {
    // organizationId	String	30	y	医院ID，医院系统身份标识
    // departmentId	String	30	y	科室ID
    // doctorId	String	30	n	医生ID
    // scheduleDate	datetime		y	出诊日期（格式：yyyy-MM-dd hh:mm:ss）
    // doctorType	Number	2	n	医生类型 参照字典 doctorType  1专家 2普通
    // periodId	Number	2	n	坐班类别 参照字典periodId
    // outpatientType	Number	2	y	门诊类型 参照字典outpatientType 1
    // lastModifyTime	datetime		n	最后修改时间（格式：yyyy-MM-dd HH:mm:ss）


    // "beginTime": "2019-07-13 08:20:00",
    // "endTime": "2019-07-13 08:25:00",
    // "regFee": 5,
    // "orderCount": 1,
    // "remainNumber": 1,
    // "serialNumber": "5",
    // "sourceState": "1",
    // "resourceId": "142094"

    // http://localhost:5000/App/Business/31/2021-06-07?doctCode=0
    if (data.organizationId === '1' && data.outpatientType === 1 && data.departmentId === '' ) {
      const result = await this.ctx.curl('http://50.168.1.224:5000/App/Business/' + data.departmentId + '/' + data.scheduleDate , {method: 'GET'} ) 
    }

  }
}
module.exports = AppointmentService;
